using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    HDDatabase Hdd;
    string strPagesetRowId;
    DataSet dseInputInfo = new DataSet();
    DataSet dsePageInfo = new DataSet();
    DataSet dseChildInputInfo = new DataSet();
    DataSet dseChildTaxFields = new DataSet();
    DataSet dseGrandTotalFields = new DataSet();
    Hashtable hashData = new Hashtable();
    //DropDownList DDChildFields = new DropDownList();
    HtmlForm frmObj;
    security hdds;
    string masteraf_row_id = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        security hdds = new security();
        if (Session["userid"] == null) Response.Redirect(hdds.SessionExpiryURL);
        if (Session["userid"].ToString() == "") Response.Redirect(hdds.SessionExpiryURL);

        //Code added by Sampath on 18-Nov-2011 for IPP Vertical privilege
        if (Request.QueryString["mn"] != null)
        {
            if (!hdds.checkObjectMenuAccess(Request.QueryString["mn"]))
            {
                showmessage();
                return;
            }
        }

        //Code added by Sampath on 15-Nov-2011 for IPP Vertical privilege
        if (Request.QueryString["PK"] != null && Request.QueryString["pgs_id"] != null)
        {
            if (!hdds.checkRecordAccess(Request.QueryString["PK"].ToString(), Request.QueryString["pgs_id"].ToString()))
            {
                showmessage();
                return;
            }
        }

        if (Request.QueryString["mn"] != null)
            hdds.logMenuClick(Request.QueryString["mn"].ToString());
        if (this.Master.FindControl("Form1") != null)
        {
            frmObj = (HtmlForm)this.Master.FindControl("Form1");
        }
        if (Request.QueryString["pgs_id"] != null)
        {
            strPagesetRowId = Request.QueryString["pgs_id"].ToString();
        }

        //lbltotals.Visible = false;

        PageInfoLoad PIL = new PageInfoLoad();
        dsePageInfo = PIL.GetPageSetPageCtrlInfo(strPagesetRowId);
        FillPageSet(dsePageInfo, strPagesetRowId);
        //if (DDChildFields.SelectedIndex > 0)
        //{
        //    hftotalsselected.Value = DDChildFields.SelectedValue;
        //}


        if (!Page.IsPostBack)
        {
            loadOwneridDropDown();
            LoadMaster();
            LoadChild();
            LoadTotalFields();
            LoadMasterFieldsDropDown();
            LoadChildFieldsDropDown();
        }

        checkEditOperation();
        isViewMode();
        

        //if (p_transaction_document_master_object.SelectedIndex > 0)
        //{
        //    hfmasterobjecttext.Value = p_transaction_document_master_object.SelectedItem.Text;
        //    hfp_transaction_document_master_object.Value = p_transaction_document_master_object.SelectedItem.Value;
        //}
        //if (p_transaction_document_child_object.SelectedIndex > 0)
        //{
        //    hfp_transaction_document_child_object.Value = p_transaction_document_child_object.SelectedItem.Value;
        //    hfchildobjecttext.Value = p_transaction_document_child_object.SelectedItem.Text;
        //}
        
        //if (Request.QueryString["PK"] == null)
        //{
            if (!Page.IsPostBack)
            {
                LoadChild();
                LoadTotalFields();
                LoadMasterFieldsDropDown();
                LoadChildFieldsDropDown();
            }
        //}


        if (!Page.IsPostBack)
        {
            getpageicons(); // Added by srivatsan
        }
            
           
        //    setdropdowndata();
            
       
    }

    private void showmessage()
    {
        try
        {
            lblmessage.Text = wrapItAsMessage("<img src=\"Images/info.gif\" alt=\"\"></img>&nbsp;You do not have sufficient access permission.", true);
            tblbtntop.Visible = false;
            tblcontrols.Visible = false;
            tblbtnbottom.Visible = false;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("showmessage: Error: " + ex.Message); 
        }
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Theme = Session["theme"].ToString();
        this.Title = "MaxBlox";
    }

    private void isViewMode()
    {
        if (HttpContext.Current.Request.QueryString["VMODE"] != null)
            if (HttpContext.Current.Request.QueryString["VMODE"] == "1")
            {
                DynamicDisplayI ddi = new DynamicDisplayI();
                ddi.disableControls(this);
               
                //lblheader.Text = "Transaction Document - View";


               // getpageicons(); // added by srivatsan on Feb 17 2009
                security HDS = new security(); // Added by srivatsan
                bool create = false; bool edit = false; bool delete = false;
                HDS.getPagesetRights(strPagesetRowId, ref create, ref edit, ref delete);
                Control cCell = null;
                if (create || edit) putEditButtons(ref ddi, ref cCell);
                if (create) putCpyButtons(ref ddi, ref cCell);
                if (delete) putDelButtons(ref ddi, ref cCell);
                if (create || edit || delete)
                {
                    if (cCell != null)
                    {
                        cCell.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;"));
                    }
                }

                ddi = null;

               
            
    


            }
    }    

    private bool LoadMaster()
    {
        Hdd = new HDDatabase();
        DataSet dsmaster = new DataSet();
        string sql="";
        bool status = false;
        ListItem list1;
        try
        {
            p_transaction_document_master_object.Items.Clear();

            list1 = new ListItem();
            list1.Text = "-- Select --";
            list1.Value = "";
            p_transaction_document_master_object.Items.Add(list1);
            //sql = "select distinct(B.object_id),B.af_row_id from sy_obj_mas_detail A, sy_pg B where A.master_obj = B.object_id order by B.object_id";
            sql = "select distinct(B.page_name),B.af_row_id from sy_obj_mas_detail A, sy_pg B where A.master_obj = B.object_id order by B.page_name";
            dsmaster = Hdd.GetDataset(sql);
            if (dsmaster != null)
            {
                if (dsmaster.Tables.Count > 0)
                {
                    if(dsmaster.Tables[0].Rows.Count>0)
                    {
                        
                        foreach (DataRow dr in dsmaster.Tables[0].Rows)
                        {
                            list1 = new ListItem();
                            //list1.Text = dr["object_id"].ToString();
                            list1.Text = dr["page_name"].ToString();
                            list1.Value = dr["af_row_id"].ToString();
                            p_transaction_document_master_object.Items.Add(list1);
                        }
                        status = true;

                           if (hfp_transaction_document_master_object.Value != "")
                                {
                                    p_transaction_document_master_object.SelectedValue = hfp_transaction_document_master_object.Value;
                                }
                    }
                }

            }

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error LoadMaster :"+ex.Message);
        }
        return status;


    }
    private bool LoadChild()
    {
        Hdd = new HDDatabase();
        DataSet dschild = new DataSet();
        bool status = false;
        string sql = "";
        ListItem list2;
        try
        {
            p_transaction_document_child_object.Items.Clear();
            list2 = new ListItem();
            list2.Text = "-- Select --";
            list2.Value = "";
            p_transaction_document_child_object.Items.Add(list2);
          
            //sql = "select distinct(B.object_id), B.af_row_id from sy_obj A, sy_pg B where A.object_id = B.object_id and A.af_row_id in (select object_id from sy_obj_mas_Detail where master_obj = '" + p_transaction_document_master_object.SelectedItem.Text + "')";
            string page_af_row_id = p_transaction_document_master_object.SelectedValue;
            if (page_af_row_id.Trim() != "")
            {
                //string object_id = dsePageInfo.Tables["Page"].Select("af_row_id = '" + page_af_row_id + "'")[0]["object_id"].ToString();
                string object_id = Hdd.GetColumnValue("select object_id from sy_pg where af_row_id = '"+page_af_row_id+"'");
                if (object_id.Trim() != "")
                {
                    sql = "select distinct(B.page_name), B.af_row_id from sy_obj A, sy_pg B where A.object_id = B.object_id and A.af_row_id in (select object_id from sy_obj_mas_Detail where master_obj = '" + object_id + "')";
                    dschild = Hdd.GetDataset(sql);
                    if (dschild != null)
                    {
                        if (dschild.Tables.Count > 0)
                        {
                            if (dschild.Tables[0].Rows.Count > 0)
                            {

                                foreach (DataRow dr in dschild.Tables[0].Rows)
                                {
                                    list2 = new ListItem();
                                    //list2.Text = dr["object_id"].ToString();
                                    list2.Text = dr["page_name"].ToString();
                                    list2.Value = dr["af_row_id"].ToString();

                                    p_transaction_document_child_object.Items.Add(list2);
                                }
                                status = true;

                                if (hfp_transaction_document_child_object.Value != "")
                                {
                                    p_transaction_document_child_object.SelectedValue = hfp_transaction_document_child_object.Value;
                                }

                            }
                        }

                    }
                }
            }

            

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error LoadChild :" + ex.Message);
        }
        finally
        {

        }
        return status;
    }
   

    private void getpageicons()// Added by srivatsan to get page icon
    {
        PageIcons icons = new PageIcons();
        HDDatabase Hdd = new HDDatabase();
        try
        {
            string strPagesetRowID = Request.QueryString["pgs_id"].ToString();

            lblheader.Text = "&nbsp;" + icons.geticontextfrompageset(strPagesetRowID, 2) + "&nbsp;" + lblheader.Text;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error :getpageicons() " + ex.Message);
            lblheader.Text = "<img src=" + PageIcons.strdefaulticon.ToString() + "></img>&nbsp;" + lblheader.Text;
        }
        finally
        {
            icons = null;
            Hdd = null;
        }

    }

    private bool checkEditOperation()
    {
        bool bEdit = false;
        if (!IsPostBack)
        {
            if (Request.QueryString["PK"] != null)
            {
                lblheader.Text = "Transaction Document - Edit";
                bEdit = true;
                if (getData())
                    setData(bEdit);
                if (Request.QueryString["VMode"] != null)
                {
                    if (Request.QueryString["Vmode"] == "1")
                    {
                        lblheader.Text = "Transaction Document - View";
                    }
                }
            }

            if (Request.QueryString["CPY"] != null)
            {
                if (Request.QueryString["CPY"] == "0")
                {
                    lblheader.Text = "Transaction Document - Copy";
                    bEdit = false;
                }
                p_transaction_document_owner_id.SelectedValue = Session["user_afrowid"].ToString();
            }
            

            if (bEdit)
            {
                btnSave.Visible = false;
                btnUpdate.Visible = true;
                btnSaveTop.Visible = false;
                btnUpdateTop.Visible = true;
                btnSaveNew.Visible = false;
                btnSaveNewTop.Visible = false;
            }
            else
            {
                btnUpdate.Visible = false;
                btnSave.Visible = true;
                btnUpdateTop.Visible = false;
                btnSaveTop.Visible = true;
                btnSaveNew.Visible = true;
                btnSaveNewTop.Visible = true;
            }
        }
        return true;
    }


    private bool UpdateData()
    {
        bool blnStatus = true;
        ScreenReader SR = new ScreenReader();
        FillInputData(dsePageInfo, dseInputInfo);
        ////dseInputInfo = SR.CaptureData(strPagesetRowId, frmObj, dsePageInfo, false);
        ExecuteCommand();
        Master MA = new Master();
        blnStatus = MA.ProcessOperation(dsePageInfo, dseInputInfo, "INSERT");
        ////if (blnStatus)
        ////{
        ////    blnStatus = UpdateTransDocTotals();
        ////    if (blnStatus)
        ////    {
        ////        blnStatus = UpdateTaxFields();
        ////        if (blnStatus)
        ////        {
        ////            blnStatus = UpdateGrandTotalFields();
        ////        }

        ////    }
        ////}

        if (blnStatus == false)
            lblmessage.Text = wrapItAsMessage("<img src=\"Images/error.gif\" alt=\"Error\"></img>&nbsp;" + "Data Update not successful. Error: " + MA.ErrorMessage,true);
        else
            lblmessage.Text = wrapItAsMessage("&nbsp;<img src='Images/info.gif' border='0'>&nbsp;Data Updated Successfully.",false);//changed by prithi on 07-Aug-2008
        return blnStatus;
    }
    private string wrapItAsMessage(string msg, bool isError)
    {
        return "<div class=\"" + (isError == true ? "mbErrorMessage" : "mbMessage") + "\">" + msg + "</div>";
    }
    private void setData(bool IsEdit)
    {
        if (dseInputInfo.Tables["p_transaction_document"] != null)
        {
            if (dseInputInfo.Tables["p_transaction_document"].Rows[0] != null)
            {
                if (IsEdit)
                {
                    if (Request.QueryString["CPY"] != null)
                    {
                        ScreenReader SR = new ScreenReader();

                        p_transaction_document_master_object.SelectedValue = dseInputInfo.Tables["p_transaction_document"].Rows[0]["master_object"].ToString();
                        p_transaction_document_child_object.SelectedValue = dseInputInfo.Tables["p_transaction_document"].Rows[0]["child_object"].ToString();                        
                        
                        
                        hfp_transaction_document_master_object.Value = dseInputInfo.Tables["p_transaction_document"].Rows[0]["master_object"].ToString();
                        hfp_transaction_document_child_object.Value = dseInputInfo.Tables["p_transaction_document"].Rows[0]["child_object"].ToString();
                        p_transaction_document_af_row_id.Value = SR.getHashKey();


                        if (dseChildInputInfo != null)
                        {
                            if (dseChildInputInfo.Tables.Count > 0)
                            {
                                if (dseChildInputInfo.Tables[0].Rows.Count > 0)
                                {
                                    DDChildFields.SelectedValue = dseChildInputInfo.Tables[0].Rows[0]["attrib_id"].ToString();
                                    hftotalsselected.Value = dseChildInputInfo.Tables[0].Rows[0]["attrib_id"].ToString();
                                }
                            }
                        }

                        hftotalsafrowid.Value = SR.getHashKey();
                        p_transaction_document_owner_id.SelectedValue = Session["user_afrowid"].ToString();
                        p_transaction_document_sort_by.Text = dseInputInfo.Tables["p_transaction_document"].Rows[0]["sort_by"].ToString();
//                        p_trans_doc_grand_total_grand_total_attrib_id.SelectedValue = dseInputInfo.Tables["p_transaction_document"].Rows[0]["grand_total"].ToString();

                        //AddToHash("grand_total", dseInputInfo.Tables["p_transaction_document"].Rows[0]["grand_total"].ToString(), ref hashData);

                        if(dseChildTaxFields!=null)
                            if(dseChildTaxFields.Tables.Count>0)
                                if (dseChildTaxFields.Tables[0].Rows.Count > 0)
                                {
                                    for (int i = 0; i < dseChildTaxFields.Tables[0].Rows.Count; i++)
                                    {
                                        if (i == 0)
                                        {
                                            //dd_tax_percent_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString();
                                            //dd_tax_master_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString();
                                            //dd_tax_child_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString();
                                            //dd_taxable_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString();

                                            AddToHash("dd_tax_percent_field1", dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_master_field1", dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_child_field1", dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString(), ref hashData);
                                            AddToHash("dd_taxable_field1", dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString(), ref hashData);
                                        }
                                        else if(i==1)
                                        {
                                            //dd_tax_percent_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString();
                                            //dd_tax_master_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString();
                                            //dd_tax_child_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString();
                                            //dd_taxable_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString();

                                            AddToHash("dd_tax_percent_field2", dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_master_field2", dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_child_field2", dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString(), ref hashData);
                                            AddToHash("dd_taxable_field2", dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString(), ref hashData);
                                        }
                                    }
                                }

                        if(dseGrandTotalFields!=null)
                            if(dseGrandTotalFields.Tables.Count>0)
                                if (dseGrandTotalFields.Tables[0].Rows.Count > 0)
                                {
                                    foreach(DataRow dr in dseGrandTotalFields.Tables[0].Rows)
                                    {
                                        AddToHash("p_trans_doc_grand_total_grand_total_attrib_id", dr["grand_total_attrib_id"].ToString(), ref hashData);
                                        AddToHash("p_trans_doc_grand_total_associated_child_attrib_id", dr["associated_child_attrib_id"].ToString(), ref hashData);
                                        //AddToHash("p_trans_doc_grand_total_active", dr["active"].ToString(), ref hashData);

                                        p_trans_doc_grand_total_active.Checked = Convert.ToBoolean(dr["active"].ToString());
                                    }
                                }

                        p_tax_fields_af_row_id_1.Value = SR.getHashKey();
                        p_tax_fields_af_row_id_2.Value = SR.getHashKey();
                        p_trans_doc_grand_total_af_row_id.Value = SR.getHashKey();
                        p_trans_doc_totals_af_row_id.Value = SR.getHashKey();


                        SR = null;
                    }
                    else
                    {
                        p_transaction_document_master_object.SelectedValue = dseInputInfo.Tables["p_transaction_document"].Rows[0]["master_object"].ToString();
                        p_transaction_document_child_object.SelectedValue = dseInputInfo.Tables["p_transaction_document"].Rows[0]["child_object"].ToString();

                        hfp_transaction_document_master_object.Value = dseInputInfo.Tables["p_transaction_document"].Rows[0]["master_object"].ToString();
                        hfp_transaction_document_child_object.Value = dseInputInfo.Tables["p_transaction_document"].Rows[0]["child_object"].ToString();
                        p_transaction_document_af_row_id.Value = dseInputInfo.Tables["p_transaction_document"].Rows[0]["af_row_id"].ToString();
                        p_transaction_document_created_by.Value = dseInputInfo.Tables["p_transaction_document"].Rows[0]["created_by"].ToString();
                        p_transaction_document_owner_id.SelectedValue = dseInputInfo.Tables["p_transaction_document"].Rows[0]["owner_id"].ToString();
                        p_transaction_document_created_at.Value = dseInputInfo.Tables["p_transaction_document"].Rows[0]["created_at"].ToString();
                        p_transaction_document_sort_by.Text = dseInputInfo.Tables["p_transaction_document"].Rows[0]["sort_by"].ToString();


                        //p_trans_doc_grand_total_grand_total_attrib_id.SelectedValue = dseInputInfo.Tables["p_transaction_document"].Rows[0]["grand_total"].ToString();
                        //AddToHash("grand_total", dseInputInfo.Tables["p_transaction_document"].Rows[0]["grand_total"].ToString(), ref hashData);
                        

                        if (dseChildInputInfo != null)
                        {
                            if (dseChildInputInfo.Tables.Count > 0)
                            {
                                if (dseChildInputInfo.Tables[0].Rows.Count > 0)
                                {
                                    DDChildFields.SelectedValue = dseChildInputInfo.Tables[0].Rows[0]["attrib_id"].ToString();
                                    hftotalsselected.Value = dseChildInputInfo.Tables[0].Rows[0]["attrib_id"].ToString();
                                    hftotalsafrowid.Value = dseChildInputInfo.Tables[0].Rows[0]["af_row_id"].ToString();
                                    p_trans_doc_totals_af_row_id.Value = dseChildInputInfo.Tables[0].Rows[0]["af_row_id"].ToString();
                                }
                            }
                        }


                        if (dseChildTaxFields != null)
                            if (dseChildTaxFields.Tables.Count > 0)
                                if (dseChildTaxFields.Tables[0].Rows.Count > 0)
                                {
                                    for (int i = 0; i < dseChildTaxFields.Tables[0].Rows.Count; i++)
                                    {
                                        if (i == 0)
                                        {
                                            //dd_tax_percent_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString();
                                            //dd_tax_master_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString();
                                            //dd_tax_child_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString();
                                            //dd_taxable_field1.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString();

                                            AddToHash("dd_tax_percent_field1", dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_master_field1", dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_child_field1", dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString(), ref hashData);
                                            AddToHash("dd_taxable_field1", dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString(), ref hashData);

                                            p_tax_fields_af_row_id_1.Value = dseChildTaxFields.Tables[0].Rows[i]["af_row_id"].ToString();
                                        }
                                        else if (i == 1)
                                        {
                                            //dd_tax_percent_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString();
                                            //dd_tax_master_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString();
                                            //dd_tax_child_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString();
                                            //dd_taxable_field2.SelectedValue = dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString();

                                            AddToHash("dd_tax_percent_field2", dseChildTaxFields.Tables[0].Rows[i]["percentage_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_master_field2", dseChildTaxFields.Tables[0].Rows[i]["master_field"].ToString(), ref hashData);
                                            AddToHash("dd_tax_child_field2", dseChildTaxFields.Tables[0].Rows[i]["child_field"].ToString(), ref hashData);
                                            AddToHash("dd_taxable_field2", dseChildTaxFields.Tables[0].Rows[i]["taxable_field"].ToString(), ref hashData);

                                            p_tax_fields_af_row_id_2.Value = dseChildTaxFields.Tables[0].Rows[i]["af_row_id"].ToString();
                                        }
                                    }
                                }


                        if (dseGrandTotalFields != null)
                            if (dseGrandTotalFields.Tables.Count > 0)
                                if (dseGrandTotalFields.Tables[0].Rows.Count > 0)
                                {
                                    foreach (DataRow dr in dseGrandTotalFields.Tables[0].Rows)
                                    {
                                        AddToHash("p_trans_doc_grand_total_grand_total_attrib_id", dr["grand_total_attrib_id"].ToString(), ref hashData);
                                        AddToHash("p_trans_doc_grand_total_associated_child_attrib_id", dr["associated_child_attrib_id"].ToString(), ref hashData);
                                        //AddToHash("p_trans_doc_grand_total_active", dr["active"].ToString(), ref hashData);

                                        p_trans_doc_grand_total_active.Checked = Convert.ToBoolean(dr["active"].ToString());
                                        p_trans_doc_grand_total_af_row_id.Value = dr["af_row_id"].ToString();
                                    }
                                }
                    }
                }
                else
                {
                    ScreenReader sc = new ScreenReader();
                    p_transaction_document_af_row_id.Value = sc.getHashKey();
                    p_transaction_document_owner_id.SelectedValue = Session["user_afrowid"].ToString();
                    hftotalsafrowid.Value = sc.getHashKey();
                    p_tax_fields_af_row_id_1.Value = sc.getHashKey();
                    p_tax_fields_af_row_id_2.Value = sc.getHashKey();
                    p_trans_doc_grand_total_af_row_id.Value = sc.getHashKey();
                    p_trans_doc_totals_af_row_id.Value = sc.getHashKey();
                   // p_sy_report_sql_report_base_id.Text = getSQLId();
                }

               // p_sy_report_sql_report_base_name.Text = dseInputInfo.Tables["p_sy_report_sql"].Rows[0]["report_base_name"].ToString();
               // p_sy_report_sql_report_base_desc.Text = dseInputInfo.Tables["p_sy_report_sql"].Rows[0]["report_base_desc"].ToString();
               // p_sy_report_sql_report_base_sql.Text = dseInputInfo.Tables["p_sy_report_sql"].Rows[0]["report_base_sql"].ToString();
            }
        }
    }

    private bool getData()
    {
        bool bReturn = true;
        try
        {
            HDDatabase hdd = new HDDatabase();
            security hdds = new security();

            string sPageKey = Request.QueryString["PK"].ToString();
            //p_sy_report_sql_af_row_id.Value = sPageKey;
            //sPageKey = hdd.GetColumnValue("select report_base_id from sy_report_sql where af_row_id = '" + sPageKey + "'");
            
            foreach (DataRow dr in dsePageInfo.Tables["page"].Rows)
            {
                string sObject_id = dr["object_id"].ToString();
                if (hdds.getObjectRightsForUser(dr["object_id"].ToString(), "read"))
                {
                    string sSql = "select * from " + dr["page_table_name"].ToString() + " where af_row_id = '" + sPageKey + "'";
                    DataTable dt = hdd.GetDataTable(sSql);
                    dt.TableName = dr["page_id"].ToString();
                    dseInputInfo.Tables.Add(dt.Copy());
                }
                else
                {
                    disableControls();
                    bReturn = false;
                }
            }


            string sql = "select * from sy_trans_doc_totals where trans_doc='" + Request.QueryString["PK"].ToString() + "'";
            dseChildInputInfo = hdd.GetDataset(sql);

            sql = "select * from sy_tax_fields where master_object = '"+Request.QueryString["PK"].ToString()+"'";
            dseChildTaxFields = hdd.GetDataset(sql);

            sql = "select * from sy_trans_doc_grand_total where trans_doc='"+Request.QueryString["PK"].ToString()+"'";
            dseGrandTotalFields = hdd.GetDataset(sql);

            
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : " + ex.Message);
            return false;
        }
        return bReturn;
    }

    private bool SaveData()
    {
        bool blnStatus = true;
        ScreenReader SR = new ScreenReader();

        FillInputData(dsePageInfo, dseInputInfo);
        //dseInputInfo = SR.CaptureData(strPagesetRowId, frmObj, dsePageInfo, true);
        Master MA = new Master();
        blnStatus = MA.ProcessOperation(dsePageInfo, dseInputInfo, "INSERT");

        //masteraf_row_id = dseInputInfo.Tables[0].Rows[0]["af_row_id"].ToString();

        //if (blnStatus)
        //{
        //    blnStatus = InsertIntoTransDocTotals(masteraf_row_id, p_transaction_document_child_object.SelectedValue.ToString(),DDChildFields.SelectedValue/*DDChildFields.SelectedValue*/);
        //    if (blnStatus)
        //    {
        //        blnStatus = InsertIntoTaxFields(masteraf_row_id);
        //        if (blnStatus)
        //        {
        //         blnStatus= InsertIntoGrandTotalFields(masteraf_row_id);
        //        }
        //    }
        //}

        if (blnStatus == false)
            lblmessage.Text = wrapItAsMessage("<img src=\"Images/error.gif\" alt=\"Error\"></img>&nbsp;" + "Data Insert not successful. Error: " + MA.ErrorMessage,true);
        else
            lblmessage.Text = wrapItAsMessage("&nbsp;<img src='Images/info.gif' border='0'>&nbsp;Data Inserted Successfully.",false);//changed by prithi on 07-Aug-2008
        return blnStatus;
    }

    private string getRefURL()
    {
        try
        {
            return "DisplayGrid.aspx?pgs_id=" + Request.QueryString["pgs_id"].ToString() + "&mn=" + Request.QueryString["mn"].ToString();
        }
        catch
        {
            return "";
        }
    }

    private void disableControls()
    {
       // p_sy_report_sql_report_base_name.Enabled = false;
       //p_sy_report_sql_report_base_desc.Enabled = false;
       // p_sy_report_sql_report_base_sql.Enabled = false;
        btnSave.Enabled = false;
        btnUpdate.Enabled = false;
    }

    protected void btnupdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (UpdateData())
            {
                string refURL = getRefURL();
                if (refURL.Length > 0)
                    Response.Redirect(refURL);
            }
            else
            {
                return;
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error SaveData :" + ex.Message);
        }
    }

    protected void btnsave_Click(object sender, EventArgs e)
    {
        try
        {
            if (SaveData())
            {

                string refURL = getRefURL();
                if (refURL.Length > 0)
                    Response.Redirect(refURL);
            }
            else
            {
                return;
            }
        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("Error SaveData :"+ex.Message);
        }

    }
    protected void btnreset_Click(object sender, EventArgs e)
    {
        string strPagesetRowId = Request.QueryString["pgs_id"].ToString();
        string strMenuRowId = Request.QueryString["mn"].ToString();
        //added for static page list view AddNew
        HDDatabase HDD = new HDDatabase();
        GenerateGrid objGD = new GenerateGrid();
        string sEditPage = objGD.getEditPage(HDD, strMenuRowId);
        //
        string strPageUrl = sEditPage + "?pgs_id=" + strPagesetRowId + "&mn=" + strMenuRowId;
        if (Request.QueryString["PK"] != null)
        {
            strPageUrl = sEditPage + "?pgs_id=" + strPagesetRowId + "&mn=" + strMenuRowId+"&PK="+Request.QueryString["PK"].ToString();
            if(Request.QueryString["CPY"]!=null)
            {
                strPageUrl = sEditPage + "?pgs_id=" + strPagesetRowId + "&mn=" + strMenuRowId+"&PK="+Request.QueryString["PK"].ToString()+"&CPY=0";
            }
        }
        
        Response.Redirect(strPageUrl);
    }
    private void setdropdowndata()
    {
        if (hfp_transaction_document_child_object.Value != "")
        {
            p_transaction_document_child_object.SelectedValue = hfp_transaction_document_child_object.Value;
            
        }
        if (hfp_transaction_document_master_object.Value != "")
        {
            p_transaction_document_master_object.SelectedValue = hfp_transaction_document_master_object.Value;
        }

    }
    protected void p_transaction_document_master_object_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadChild();
        LoadMasterFieldsDropDown();
        LoadChildFieldsDropDown();
    }

    private void setchilddropdown(string value)
    {
        p_transaction_document_child_object.SelectedValue = value;
    }
    private void setmasterdropdown(string value)
    {
        p_transaction_document_master_object.SelectedValue = value;
    }
    private bool fillchilddropdown()
    {
        Hdd = new HDDatabase();
        DataSet dschild = new DataSet();
        bool status = false;
        string sql = "select distinct(B.object_id), B.af_row_id from sy_obj A, sy_pg B where A.object_id = B.object_id and A.af_row_id in (select object_id from sy_obj_mas_Detail where master_obj = '" + hfmasterobjecttext.Value + "')";
        ListItem list2;

        try
        {
            
            p_transaction_document_child_object.Items.Clear();
            list2 = new ListItem();
            list2.Text = "-- Select --";
            list2.Value = "";
            p_transaction_document_child_object.Items.Add(list2);

            dschild = Hdd.GetDataset(sql);
            if (dschild != null)
            {
                if (dschild.Tables.Count > 0)
                {
                    if (dschild.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in dschild.Tables[0].Rows)
                        {
                            list2 = new ListItem();
                            list2.Text = dr["object_id"].ToString();
                            list2.Value = dr["af_row_id"].ToString();

                            p_transaction_document_child_object.Items.Add(list2);
                        }

                        status = true;

                        if (hfp_transaction_document_child_object.Value != null)
                        {
                            if (hfp_transaction_document_child_object.Value != "")
                            {
                                p_transaction_document_child_object.SelectedValue = hfp_transaction_document_child_object.Value;
                            }

                        }
                        
                    }
                }

            }


        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error LoadChild :" + ex.Message);
        }
        finally
        {

        }
        return status;
    }

    protected void btnupdatetop_Click(object sender, EventArgs e)
    {
        try
        {
            UpdateData();

            string refURL = getRefURL();
            if (refURL.Length > 0)
                Response.Redirect(refURL);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error SaveData :" + ex.Message);
        }
    }

    protected void btnsavetop_Click(object sender, EventArgs e)
    {
        try
        {
            if (SaveData())
            {
                string refURL = getRefURL();
                if (refURL.Length > 0)
                    Response.Redirect(refURL);
            }
            else
            {
                return;
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error SaveData :" + ex.Message);
        }

    }
    protected void btnresettop_Click(object sender, EventArgs e)
    {
        string strPagesetRowId = Request.QueryString["pgs_id"].ToString();
        string strMenuRowId = Request.QueryString["mn"].ToString();
        //added for static page list view AddNew
        HDDatabase HDD = new HDDatabase();
        GenerateGrid objGD = new GenerateGrid();
        string sEditPage = objGD.getEditPage(HDD, strMenuRowId);
        //
        string strPageUrl = sEditPage + "?pgs_id=" + strPagesetRowId + "&mn=" + strMenuRowId;
        Response.Redirect(strPageUrl);
    }
    protected void btnsavenewtop_Click(object sender, EventArgs e)
    {
        saveandnew();
    }

    private void saveandnew()
    {
        try
        {
            if (SaveData())
            {
                string ret = HttpContext.Current.Request.Url.AbsolutePath + "?pgs_id=" + HttpContext.Current.Request.QueryString["pgs_id"] + "&mn=" + HttpContext.Current.Request.QueryString["mn"];

                if (Request.QueryString["MK"] != null && ret != "")
                    ret = ret + "&MK=" + Request.QueryString["MK"];

                if (ret != "")
                    Response.Redirect(ret, true);
                else
                    lblmessage.Text = wrapItAsMessage("Data Inserted Successfully",false);
            }
            else
            {
                Master objMaster = new Master();
                if (objMaster.DeleteMessage != "")
                {
                    lblmessage.Text = wrapItAsMessage("<img src=\"Images/error.gif\" alt=\"Error\"></img>&nbsp;" + "Data Insert not successful. Error: " + objMaster.DeleteMessage,true);
                    objMaster.DeleteMessage = "";
                }
                else
                {
                   // lblmessage.Text = "<img src=\"Images/error.gif\" alt=\"Error\"></img>&nbsp;" + "Data Insert not successful. Error:" + ErrorMessage;
                }
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error saveandnew() :"+ ex.Message);
        }

    }
    protected void btnsavenew_Click(object sender, EventArgs e)
    {
        saveandnew();
    }


    private void putCpyButtons(ref DynamicDisplayI DDI, ref Control cCell) // implemented by Prithi on 4-Nov-2008
    {
        try
        {
            Control cc = cCell;
            if (cc == null)
                cc = DDI.FindControl(frmObj.Controls, "addButtons");//if multiple records page this will not be there
            if (cc == null) return;

            ImageButton b = new ImageButton();
            b = new ImageButton();
            b.ID = "btnCpyTop";
            b.ToolTip = "Copy";
            b.CausesValidation = false;
            b.ImageUrl = "images/mb_button_copy.gif";
            //b.Attributes.Add("OnClick", "javascript:disableThis('ctl00_ContentPlaceHolder3_btnCpyTop','Loading...');");
            b.Click += new ImageClickEventHandler(btnCpy_Click);
            //b.UseSubmitBehavior = false;
            cc.Controls.Add(b);

            //htc.VAlign = "right";
            cc.Controls.Add(new LiteralControl("&nbsp;"));
            //htc.Attributes.Add("style", "padding-left:0px");
            cCell = cc;
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in putHeaderButtons. Error: " + e.Message);
        }
    }

    private void putDelButtons(ref DynamicDisplayI DDI, ref Control cCell) // Implemented by Prithi on 4-Nov-2008
    {
        try
        {
            Control cc = cCell;
            if (cc == null)
                cc = DDI.FindControl(frmObj.Controls, "addButtons");//if multiple records page this will not be there
            if (cc == null) return;

            ImageButton b = new ImageButton();
            b = new ImageButton();
            b.ID = "btnDelTop";
            b.ToolTip = "Delete";
            b.CausesValidation = false;
            b.ImageUrl = "images/mb_button_delete.gif";
            //b.Attributes.Add("OnClick", "javascript:disableThis('ctl00_ContentPlaceHolder3_btnDelTop','Deleting...');");           
            DataTable dtaPage = new DataTable();
            dtaPage = dsePageInfo.Tables["Page"];
            string strtable = dtaPage.Rows[0]["page_table_name"].ToString();
            string strsql = "select * from " + strtable + " where af_row_id = '" + Request.QueryString["PK"].ToString() + "'";
            HDDatabase hd = new HDDatabase();
            DataTable drr = new DataTable();
            drr = hd.GetDataTable(strsql);
            string strname = drr.Rows[0][2].ToString();
            // if (hasChild(ref dsePageInfo))
            //  b.OnClientClick = @"javascript:if(!confirm(""Are you sure you want to delete this Record ?\r Note: Child records will also be deleted."")){return false;};";
            // else
            b.OnClientClick = @"javascript:if(!confirm(""Are you sure you want to delete this Record ?"")){return false;};";
            b.Click += new ImageClickEventHandler(btnDel_Click);
            cc.Controls.Add(b);
            cc.Controls.Add(new LiteralControl("&nbsp;"));
            cCell = cc;
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in putHeaderButtons. Error: " + e.Message);
        }
    }

    private void putEditButtons(ref DynamicDisplayI DDI, ref Control cCell)//implemented by sanal on 30th July 2008 , changed by Prithi on 4-Nov-2008
    {
        try
        {
            Control cc = cCell;
            if (cc == null)
                cc = DDI.FindControl(frmObj.Controls, "addButtons");//if multiple records page this will not be there
            if (cc == null) return;
            ImageButton b = new ImageButton();
            b = new ImageButton();
            b.ID = "btnEditTop";
            b.ImageUrl = "images/mb_button_edit.gif";
            b.ToolTip = "Edit";
            b.CausesValidation = false;
            // b.Attributes.Add("OnClick", "javascript:disableThis('ctl00_ContentPlaceHolder3_btnEditTop','Loading...');");
            b.Click += new ImageClickEventHandler(btnEdit_Click);
            //b.UseSubmitBehavior = false;
            cc.Controls.Add(b);
            cc.Controls.Add(new LiteralControl("&nbsp;"));
            cCell = cc;
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in putHeaderButtons. Error: " + e.Message);
        }
    }


    protected void btnDel_Click(object sender, EventArgs e)  // Implemented by Prithi on 4-Nov-2008 
    {
        try
        {


            string strDelKey = "";
            string strPagesetRowId = "";
            if (Request.QueryString["pgs_id"] != null)
            {
                strPagesetRowId = Request.QueryString["pgs_id"].ToString();
            }

            if (Request.QueryString["PK"] != null)
            {
                strDelKey = Request.QueryString["PK"].ToString().Replace("'", "''");
            }
            if (strDelKey != "")
            {
                string ret = getURL();
                if (ret != "")
                {
                    if (ret.Contains("DynamicScreen"))
                    {
                        DataSet dsePageInfo = new DataSet();
                        PageInfoLoad PIL = new PageInfoLoad();
                        dsePageInfo = PIL.GetPageSetPageCtrlInfo(strPagesetRowId);

                        Master MA = new Master();
                        bool blnStatus = MA.ProcessOperation(dsePageInfo, strDelKey, "DELETE");
                        if (blnStatus)
                        {
                            string ret1 = getURL();
                            if (ret1 != "")
                            {
                                Response.Redirect(ret1, true);

                            }

                        }
                    }
                    else if (ret.Contains("DisplayGrid") || ret.Contains("Search"))
                    {
                        //ret = ret + "&DPK=" + strDelKey + @"";
                        //Response.Redirect(ret, true);

                        DataSet dsePageInfo = new DataSet();
                        PageInfoLoad PIL = new PageInfoLoad();
                        dsePageInfo = PIL.GetPageSetPageCtrlInfo(strPagesetRowId);

                        Master MA = new Master();
                        Session["DPK"] = strDelKey;
                        bool blnStatus = MA.ProcessOperation(dsePageInfo, strDelKey, "DELETE");
                        Session["DPK"] = null;

                        Response.Redirect(ret, true);

                    }
                    else Response.Redirect(ret, true);
                }

            }

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error in btnDel_Click. Error: " + ex.Message);
        }

    }

    protected void btnEdit_Click(object sender, EventArgs e) // Implemented by Prithi on 4-Nov-2008 
    {
        try
        {
            ArrayList alURLs = new ArrayList();
            alURLs = (ArrayList)HttpContext.Current.Session["URLs"];
            if (alURLs[alURLs.Count - 1].ToString().Contains("&VMODE=1"))
            {
                if (Request.QueryString["PK"] == HttpContext.Current.Session["PK"])
                {
                    alURLs.RemoveAt(alURLs.Count - 1);
                }
            }
            string url = Request.Url.ToString().ToLower();
            url = url.ToString().Replace("&vmode=1", "").Replace("&vmode=0", "");
            if (url != "")
            {
                if (Request.QueryString["PK"] != null)
                {
                    if (HttpContext.Current.Session["PK"] != null)
                    {
                        if (ischild(strPagesetRowId))
                        {
                            url = url + "&MK=" + HttpContext.Current.Session["PK"].ToString() + @"";
                        }
                    }
                    else
                    {
                        url = url;
                    }

                    Response.Redirect(url, true);
                }
            }

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error in btnEdit_Click function: " + ex.Message);
        }

    }
    protected void btnCpy_Click(object sender, EventArgs e)  // Implemented by Prithi on 4-Nov-2008 
    {
        try
        {
            ArrayList alURLs = new ArrayList();
            alURLs = (ArrayList)HttpContext.Current.Session["URLs"];
            if (alURLs[alURLs.Count - 1].ToString().Contains("&VMODE=1"))
            {
                if (Request.QueryString["PK"] == HttpContext.Current.Session["PK"])
                {
                    alURLs.RemoveAt(alURLs.Count - 1);
                }
            }
            string url = Request.Url.ToString();
            url = url.ToString().Replace("&vmode=1", "").Replace("&vmode=0", "").Replace("&VMODE=1", "").Replace("&VMODE=0", "");
            System.Text.StringBuilder txt = new System.Text.StringBuilder(url);
            txt.Append("&CPY=0");
            url = txt.ToString();
            if (url != "")
            {
                if (Request.QueryString["PK"] != null)
                {
                    if (HttpContext.Current.Session["PK"] != null)
                    {
                        if (ischild(strPagesetRowId))
                        {
                            url = url + "&MK=" + HttpContext.Current.Session["PK"].ToString() + @"";
                        }
                    }
                    else
                    {
                        url = url;
                    }

                    Response.Redirect(url, true);
                }
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error in btnCpy_Click function: " + ex.Message);
        }
    }

    private string getURL()//implemented by sanal on 13 Dec 2007
    {
        try
        {
            //return hfRequestURL.Value;
            if (HttpContext.Current.Session["URLs"] == null)
                return "";//hfRequestURL.Value;
            else
            {
                return Navigator.getURL(Request.Url.ToString());
            }
            //return Session["naviURL"].ToString();
            //return "DisplayGrid.aspx?mn=" + Request.QueryString["mn"].ToString() + "&pgs_id=" + Request.QueryString["pgs_id"].ToString();
        }
        catch (Exception e)
        {
            return "";
        }
    }

    private bool ischild(string strPagesetRowId)
    {
        bool schild = false;
        try
        {
            string sPage_id = "";
            string sObject_Id = "";
            string sAF_ROW_ID = "";
            string obj_id = "";
            string ischild = "";
            HDDatabase HDD = new HDDatabase();
            string sSql = "select af_row_id from sy_pgs where pageset_id='" + strPagesetRowId + "'";
            //string sSql = "select pg.object_id from sy_pg pg inner join sy_pgs_mem pgs_mem on pg.page_id=pgs_mem.page_id inner join sy_pgs pgs on pgs.af_row_id=pgs_mem.pageset_id where pgs.pageset_id = '" + strPagesetRowId + "'";
            //sAF_ROW_ID = HDD.GetColumnValue(sSql);
            sSql = "select page_id from sy_pgs_mem where pageset_id='" + strPagesetRowId + "'";
            sPage_id = HDD.GetColumnValue(sSql);
            sSql = "select object_id from sy_pg where page_id='" + sPage_id + "'";
            sObject_Id = HDD.GetColumnValue(sSql);
            sSql = "select is_child from sy_obj where object_id='" + sObject_Id + "'";
            ischild = HDD.GetColumnValue(sSql);
            sSql = "select object_id from sy_obj_mas_detail where object_id='" + ischild + "'";
            obj_id = HDD.GetColumnValue(sSql);
            if (obj_id != "")
            {
                if (obj_id == ischild)
                {
                    schild = true;
                    return schild;
                }
            }
            return schild;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("IsChild : Error : " + ex.Message);
        }
        return schild;
    }
    protected void p_transaction_document_child_object_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadTotalFields();
        LoadChildFieldsDropDown();
    }

    private void LoadTotalFields()
    {
        HDDatabase Hdd = new HDDatabase();
        DataSet ds = new DataSet();
        HtmlTable ht = new HtmlTable();
        HtmlTableCell htc = new HtmlTableCell();
        HtmlTableRow htr = new HtmlTableRow();

        int rowcount = 1;
       
        ListItem list1 = null;
        try
        {
            DDChildFields.Items.Clear();
            string sql = "select * from sy_pg_ctr where page_id = '"+p_transaction_document_child_object.SelectedValue+"' and control_visible =1";
            ds = Hdd.GetDataset(sql);
            list1 = new ListItem("-- Select --", "");
            DDChildFields.Items.Add(list1);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
               list1 = new ListItem(dr["control_name"].ToString(), dr["control_attrib_id"].ToString());
                DDChildFields.Items.Add(list1);
               
             
            }
            DDChildFields.CssClass = "Controlfont";
            //DDChildFields.ID = "p_trans_doc_totals_attrib_id_" + rowcount;

            //HiddenField hf = new HiddenField();
            //hf.ID = "hf_p_trans_doc_totals_attrib_id_" + rowcount;

            //htc.Controls.Add(hf);

            //if (this.Form.FindControl("hf_p_trans_doc_totals_attrib_id_" + rowcount) != null)
            //{

            //}
            if (hftotalsselected.Value != "")
            {
                DDChildFields.SelectedValue = hftotalsselected.Value;
            }

            //htc.Controls.Add(DDChildFields);

            //htr.Controls.Add(htc);
            //ht.Controls.Add(htr);
            //lbltotals.Visible = true;
            //tdtotals.Controls.Add(ht);
            
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error LoadTotalFields :"+ex.Message);
        }
        
    }

    private bool InsertIntoTransDocTotals(string trans_doc,string page_id,string attrib_id)
    {
        HDDatabase Hdd = new HDDatabase();
        SqlCommand cmd = new SqlCommand();
        ScreenReader SR = new ScreenReader();
        bool status = false;
        try
        {
           cmd.CommandText = "insert into sy_trans_doc_totals(trans_doc,page_id,attrib_id,af_row_id,created_by,owner_id,created_at,modified_by,modified_at)"+
                              "values('" + trans_doc + "','" + page_id + "','" + attrib_id + "','" + SR.getHashKey() + "','" + Session["user_afrowid"].ToString() + "','" + Session["user_afrowid"].ToString() + "','" + System.DateTime.UtcNow + "','" + Session["user_afrowid"].ToString() + "','" + System.DateTime.UtcNow + "')";

            Hdd.ExecuteCmdObj(cmd);

            status = true;

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error InsertIntoTransDocTotals :"+ex.Message);
        }
        return status;
    }

    private bool InsertIntoTaxFields(string master_af_row_id)
    {
        bool status = false;
        HDDatabase HDD = new HDDatabase();
        SqlCommand cmd = new SqlCommand();
        ScreenReader SR = new ScreenReader();
        try
        {
                cmd.CommandText = "insert into sy_tax_fields"+
                    "(percentage_field,master_field,child_field,master_object,taxable_field,master_page_id,child_page_id,af_row_id,created_by,modified_by,owner_id,created_at,modified_at,seq)"+"values('"+dd_tax_percent_field1.SelectedValue+"','"+dd_tax_master_field1.SelectedValue+"','"+dd_tax_child_field1.SelectedValue+"','"+master_af_row_id+"','"+dd_taxable_field1.SelectedValue+"','"+p_transaction_document_master_object.SelectedValue+"','"+p_transaction_document_child_object.SelectedValue+"','"+SR.getHashKey()+"','"+Session["user_afrowid"].ToString()+"','"+Session["user_afrowid"].ToString()+"','"+p_transaction_document_owner_id.SelectedValue+"','"+DateTime.UtcNow+"','"+DateTime.UtcNow+"',1)";
                HDD.ExecuteCmdObj(cmd);
                status = true;
            
                cmd.CommandText = "insert into sy_tax_fields" +
                    "(percentage_field,master_field,child_field,master_object,taxable_field,master_page_id,child_page_id,af_row_id,created_by,modified_by,owner_id,created_at,modified_at,seq)" + "values('" + dd_tax_percent_field2.SelectedValue + "','" + dd_tax_master_field2.SelectedValue + "','" + dd_tax_child_field2.SelectedValue + "','" + master_af_row_id + "','" + dd_taxable_field2.SelectedValue + "','" + p_transaction_document_master_object.SelectedValue + "','" + p_transaction_document_child_object.SelectedValue + "','" + SR.getHashKey() + "','" + Session["user_afrowid"].ToString() + "','" + Session["user_afrowid"].ToString() + "','" + p_transaction_document_owner_id.SelectedValue + "','" + DateTime.UtcNow + "','" + DateTime.UtcNow + "',2)";
                HDD.ExecuteCmdObj(cmd);
                status = true;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : InsertIntoTaxFields :" + ex.Message);
        }
        finally
        {
            HDD = null;
            cmd = null;
            SR = null;
        }
        return status;
    }

    private bool InsertIntoGrandTotalFields(string master_af_row_id)
    {
        bool status = false;
        HDDatabase HDD = new HDDatabase();
        SqlCommand cmd = new SqlCommand();
        ScreenReader SR = new ScreenReader();
        try
        {
            cmd.CommandText = "insert into [sy_trans_doc_grand_total] ([page_id],[grand_total_attrib_id],[associated_child_attrib_id],[seq],[active],[af_row_id],[created_by],[created_at],[modified_at],[modified_by],[owner_id],[trans_doc]) values ('"+p_transaction_document_master_object.SelectedValue+"','"+p_trans_doc_grand_total_grand_total_attrib_id.SelectedValue+"','"+p_trans_doc_grand_total_associated_child_attrib_id.SelectedValue+"',1,'"+Convert.ToBoolean(p_trans_doc_grand_total_active.Checked)+"','"+SR.getHashKey()+"','"+Session["user_afrowid"].ToString()+"','"+DateTime.UtcNow+"','"+DateTime.UtcNow+"','"+Session["user_afrowid"].ToString()+"','"+p_transaction_document_owner_id.SelectedValue+"','"+master_af_row_id+"');";
            HDD.ExecuteCmdObj(cmd);
            status = true;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : InsertIntoTaxFields :" + ex.Message);
        }
        finally
        {
            HDD = null;
            cmd = null;
            SR = null;
        }
        return status;
    }

    private bool UpdateTransDocTotals()
    {
        HDDatabase HDD = new HDDatabase();
        SqlCommand cmd = new SqlCommand();
        bool status = false;
        string pk="";
        try
        {
            if(HttpContext.Current.Request["PK"]!=null)
            {
                pk = Request.QueryString["PK"].ToString();
            }
            cmd.CommandText = "Update sy_trans_doc_totals set trans_doc = '"+pk+"',page_id='"+p_transaction_document_child_object.SelectedValue+"',attrib_id='"+DDChildFields.SelectedValue+"' where af_row_id = '"+hftotalsafrowid.Value+"'";
            HDD.ExecuteCmdObj(cmd);
            status = true;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error UpdateTransDocTotals :"+ex.Message);
        }
        return status;
    }

    private bool UpdateTaxFields()
    {
        bool status = false;
        HDDatabase HDD = new HDDatabase();
        SqlCommand cmd = new SqlCommand();
        string pk = "";
        try
        {
            if (HttpContext.Current.Request["PK"] != null)
            {
                pk = Request.QueryString["PK"].ToString();
            }

            cmd.CommandText = "Update sy_tax_fields set percentage_field = '"+dd_tax_percent_field1.SelectedValue+"',master_field='"+dd_tax_master_field1.SelectedValue+"',child_field='"+dd_tax_child_field1.SelectedValue+"',modified_by='"+Session["user_afrowid"].ToString()+"',taxable_field='"+dd_taxable_field1.SelectedValue+"',master_page_id ='"+p_transaction_document_master_object.SelectedValue+"',child_page_id = '"+p_transaction_document_child_object.SelectedValue+"' where seq=1 and master_object='"+pk+"'";
            HDD.ExecuteCmdObj(cmd);

            cmd.CommandText = "Update sy_tax_fields set percentage_field = '" + dd_tax_percent_field2.SelectedValue + "',master_field='" + dd_tax_master_field2.SelectedValue + "',child_field='" + dd_tax_child_field2.SelectedValue + "',modified_by='" + Session["user_afrowid"].ToString() + "',taxable_field='" + dd_taxable_field2.SelectedValue + "',master_page_id ='" + p_transaction_document_master_object.SelectedValue + "',child_page_id = '" + p_transaction_document_child_object.SelectedValue + "' where seq=2 and master_object='" + pk + "'";
            HDD.ExecuteCmdObj(cmd);

            status = true;
        }
        catch
        {
        }
        return status;
    }

    private bool UpdateGrandTotalFields()
    {
        bool status = false;
        HDDatabase HDD = new HDDatabase();
        SqlCommand cmd = new SqlCommand();
        string pk = "";
        try
        {
            if (HttpContext.Current.Request["PK"] != null)
            {
                pk = Request.QueryString["PK"].ToString();
            }

            cmd.CommandText = "update sy_trans_doc_grand_total set page_id = '"+p_transaction_document_master_object.SelectedValue+"', grand_total_attrib_id = '"+p_trans_doc_grand_total_grand_total_attrib_id.SelectedValue+"', associated_child_attrib_id='"+p_trans_doc_grand_total_associated_child_attrib_id.SelectedValue+"',seq=1,active='"+Convert.ToBoolean(p_trans_doc_grand_total_active.Checked)+"',modified_by='"+Session["user_afrowid"].ToString()+"',modified_at='"+DateTime.UtcNow+"',owner_id = '"+p_transaction_document_owner_id.SelectedValue+"',trans_doc='"+pk+"' where trans_doc = '"+pk+"' and seq=1";
            HDD.ExecuteCmdObj(cmd);

            status = true;
        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("Error : UpdateGrandTotalFields : "+ ex.Message);
        }
        return status;
    }

    private void loadOwneridDropDown() // Added by srivatsan for owner id change
    {
        HDDatabase Hdd = new HDDatabase();
        BindControl BC = new BindControl();
        //DataSet ds = new DataSet();
        DataTable dt = null;
        try
        {
            string strBindRowID = Hdd.GetColumnValue("select mem_value3 from sy_acr_mem where acr_id = 'cee69885ce44589c' and mem_id = 'owner_id'");
            BC.Bind(strBindRowID, p_transaction_document_owner_id, ref dt,null);
            if (Session["user_afrowid"].ToString() != "e51e0327224a2eb9")
            {
                ListItem list = p_transaction_document_owner_id.Items.FindByValue("e51e0327224a2eb9");
                if (list != null)
                {
                    p_transaction_document_owner_id.Items.Remove(list);
                }
                //p_transaction_document_owner_id.Items.Add(new ListItem(Session["userid"].ToString(), Session["user_afrowid"].ToString()));
            }

            if (Request.QueryString["PK"] == null)
            {
                p_transaction_document_owner_id.SelectedValue = Session["user_afrowid"].ToString();
            }
            if (Request.QueryString["PK"] != null)
            {
                if (Request.QueryString["CPY"] != null)
                {
                    p_transaction_document_owner_id.SelectedValue = Session["user_afrowid"].ToString();
                }

            }

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : loadOwneridDropDown :" + ex.Message);
        }
        finally
        {
            Hdd = null;
            BC = null;
            dt = null;
        }


    }

    private DataSet GetControlList(string tablename, string columnname,string strwhere,string orderby)
    {
        DataSet dsresult = new DataSet();
        HDDatabase HDD = new HDDatabase();
        try
        {
            string sql = "select " + columnname + " from " + tablename + " where " + strwhere + "";
            if (orderby.Trim() != "")
                sql += " order by " + orderby + "";

            dsresult = HDD.GetDataset(sql);
        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("Error : GetControlList :"+ ex.Message);
        }
        finally
        {
            HDD = null;
        }
        return dsresult;
    }
    private void LoadMasterFieldsDropDown()
    {
        try
        {
            p_trans_doc_grand_total_grand_total_attrib_id.Items.Clear();
            dd_tax_master_field2.Items.Clear();
            dd_tax_master_field1.Items.Clear();
            dd_tax_percent_field1.Items.Clear();
            dd_tax_percent_field2.Items.Clear();

            DataSet ds = GetControlList("sy_pg_ctr", "control_id,control_name", "page_id = '" + p_transaction_document_master_object.SelectedValue + "'","control_name");
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        p_trans_doc_grand_total_grand_total_attrib_id.DataSource = ds;
                        p_trans_doc_grand_total_grand_total_attrib_id.DataTextField = "control_name";
                        p_trans_doc_grand_total_grand_total_attrib_id.DataValueField = "control_id";
                        p_trans_doc_grand_total_grand_total_attrib_id.DataBind();

                        dd_tax_master_field1.DataSource = ds;
                        dd_tax_master_field1.DataTextField = "control_name";
                        dd_tax_master_field1.DataValueField = "control_id";
                        dd_tax_master_field1.DataBind();

                        dd_tax_master_field2.DataSource = ds;
                        dd_tax_master_field2.DataTextField = "control_name";
                        dd_tax_master_field2.DataValueField = "control_id";
                        dd_tax_master_field2.DataBind();

                        dd_tax_percent_field1.DataSource = ds;
                        dd_tax_percent_field1.DataTextField = "control_name";
                        dd_tax_percent_field1.DataValueField = "control_id";
                        dd_tax_percent_field1.DataBind();

                        dd_tax_percent_field2.DataSource = ds;
                        dd_tax_percent_field2.DataTextField = "control_name";
                        dd_tax_percent_field2.DataValueField = "control_id";
                        dd_tax_percent_field2.DataBind();

                        p_trans_doc_grand_total_grand_total_attrib_id.ClearSelection();
                        dd_tax_master_field1.ClearSelection();
                        dd_tax_master_field2.ClearSelection();
                        dd_tax_percent_field1.ClearSelection();
                        dd_tax_percent_field2.ClearSelection();
                        p_trans_doc_grand_total_grand_total_attrib_id.ClearSelection();

                        if (hashData != null)
                        {
                            if(hashData.ContainsKey("grand_total"))
                                if (hashData["grand_total"].ToString() != "")
                                {
                                    p_trans_doc_grand_total_grand_total_attrib_id.SelectedValue = hashData["grand_total"].ToString();
                                }

                            if (hashData.ContainsKey("dd_tax_master_field1"))
                                if (hashData["dd_tax_master_field1"].ToString() != "")
                                {
                                    dd_tax_master_field1.SelectedValue = hashData["dd_tax_master_field1"].ToString();
                                }

                            if (hashData.ContainsKey("dd_tax_master_field2"))
                                if (hashData["dd_tax_master_field2"].ToString() != "")
                                {
                                    dd_tax_master_field2.SelectedValue = hashData["dd_tax_master_field2"].ToString();
                                }

                            if (hashData.ContainsKey("dd_tax_percent_field1"))
                                if (hashData["dd_tax_percent_field1"].ToString() != "")
                                {
                                    dd_tax_percent_field1.SelectedValue = hashData["dd_tax_percent_field1"].ToString();
                                }

                            if (hashData.ContainsKey("dd_tax_percent_field2"))
                                if (hashData["dd_tax_percent_field2"].ToString() != "")
                                {
                                    dd_tax_percent_field2.SelectedValue = hashData["dd_tax_percent_field2"].ToString();
                                }
                            if (hashData.ContainsKey("p_trans_doc_grand_total_grand_total_attrib_id"))
                                if (hashData["p_trans_doc_grand_total_grand_total_attrib_id"].ToString() != "")
                                {
                                    p_trans_doc_grand_total_grand_total_attrib_id.SelectedValue = hashData["p_trans_doc_grand_total_grand_total_attrib_id"].ToString();
                                }

                        }

                        
                    }
                }
            }

            ListItem emptyitem = new ListItem("--Select--", "");

            p_trans_doc_grand_total_grand_total_attrib_id.Items.Insert(0, new ListItem("--Select--", ""));
            dd_tax_master_field2.Items.Insert(0, new ListItem("--Select--", ""));
            dd_tax_master_field1.Items.Insert(0, new ListItem("--Select--", ""));
            dd_tax_percent_field1.Items.Insert(0, new ListItem("--Select--", ""));
            dd_tax_percent_field2.Items.Insert(0, new ListItem("--Select--", ""));

        }
        catch
        {
        }
    }

    private void LoadChildFieldsDropDown()
    {
        DataSet ds = new DataSet();
        try
        {
            dd_tax_child_field1.Items.Clear();
            dd_tax_child_field2.Items.Clear();
            dd_taxable_field1.Items.Clear();
            dd_taxable_field2.Items.Clear();
            p_trans_doc_grand_total_associated_child_attrib_id.Items.Clear();

            ds = GetControlList("sy_pg_ctr", "control_id,control_name", "page_id = '" + p_transaction_document_child_object.SelectedValue + "'","control_name");

            if(ds!=null)
                if(ds.Tables.Count>0)
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dd_tax_child_field1.DataSource = ds;
                        dd_tax_child_field1.DataTextField = "control_name";
                        dd_tax_child_field1.DataValueField = "control_id";
                        dd_tax_child_field1.DataBind();

                        dd_tax_child_field2.DataSource = ds;
                        dd_tax_child_field2.DataTextField = "control_name";
                        dd_tax_child_field2.DataValueField = "control_id";
                        dd_tax_child_field2.DataBind();

                        dd_taxable_field1.DataSource = ds;
                        dd_taxable_field1.DataTextField = "control_name";
                        dd_taxable_field1.DataValueField = "control_id";
                        dd_taxable_field1.DataBind();

                        dd_taxable_field2.DataSource = ds;
                        dd_taxable_field2.DataTextField = "control_name";
                        dd_taxable_field2.DataValueField = "control_id";
                        dd_taxable_field2.DataBind();

                        p_trans_doc_grand_total_associated_child_attrib_id.DataSource = ds;
                        p_trans_doc_grand_total_associated_child_attrib_id.DataTextField = "control_name";
                        p_trans_doc_grand_total_associated_child_attrib_id.DataValueField = "control_id";
                        p_trans_doc_grand_total_associated_child_attrib_id.DataBind();

                        dd_tax_child_field1.ClearSelection();
                        dd_tax_child_field2.ClearSelection();
                        dd_taxable_field1.ClearSelection();
                        dd_taxable_field2.ClearSelection();
                        p_trans_doc_grand_total_associated_child_attrib_id.ClearSelection();

                        if (hashData != null)
                        {
                            if (hashData.ContainsKey("dd_tax_child_field1"))
                                if (hashData["dd_tax_child_field1"].ToString() != "")
                                {
                                    dd_tax_child_field1.SelectedValue = hashData["dd_tax_child_field1"].ToString();
                                }
                            if (hashData.ContainsKey("dd_tax_child_field2"))
                                if (hashData["dd_tax_child_field2"].ToString() != "")
                                {
                                    dd_tax_child_field2.SelectedValue = hashData["dd_tax_child_field2"].ToString();
                                }
                            if (hashData.ContainsKey("dd_taxable_field1"))
                                if (hashData["dd_taxable_field1"].ToString() != "")
                                {
                                    dd_taxable_field1.SelectedValue = hashData["dd_taxable_field1"].ToString();
                                }
                            if (hashData.ContainsKey("dd_taxable_field2"))
                                if (hashData["dd_taxable_field2"].ToString() != "")
                                {
                                    dd_taxable_field2.SelectedValue = hashData["dd_taxable_field2"].ToString();
                                }
                            if (hashData.ContainsKey("p_trans_doc_grand_total_associated_child_attrib_id"))
                                if (hashData["p_trans_doc_grand_total_associated_child_attrib_id"].ToString() != "")
                                {
                                    p_trans_doc_grand_total_associated_child_attrib_id.SelectedValue = hashData["p_trans_doc_grand_total_associated_child_attrib_id"].ToString();
                                }

                            

                        }
                    }
            ListItem emptyitem = new ListItem("--Select--", "");
            dd_tax_child_field1.Items.Insert(0, new ListItem("--Select--", ""));
            dd_tax_child_field2.Items.Insert(0, new ListItem("--Select--", ""));
            dd_taxable_field1.Items.Insert(0, new ListItem("--Select--", ""));
            dd_taxable_field2.Items.Insert(0, new ListItem("--Select--", ""));
            p_trans_doc_grand_total_associated_child_attrib_id.Items.Insert(0, new ListItem("--Select--", ""));
            
        }
        catch
        {

        }
        finally
        {
            ds = null;
        }
    }

    private void AddToHash(string key, string value,ref Hashtable hsh)
    {
        try
        {
            if (hsh != null)
            {
                if (!hsh.Contains(key))
                {
                    hsh.Add(key, value);
                }

            }
        }
        catch
        {

        }
    }
    private DataSet FillPageSet(DataSet dsPageInfo, string strPagesetRowId)
    {
        DataSet ds = new DataSet();
        try
        {
            PageInfoLoad PIL = new PageInfoLoad();
          //  trans_doc_grand_total
//trans_doc_totals
//tax_fields
            DataSet dsPageInfo1 = PIL.GetPageSetPageCtrlInfo("fcfb29cd3d144617"); // trans_doc_grand_total
            //dsPageInfo1.Tables["Page"].Rows[0]["multiple_recs_page"] = true;
            DataSet dsPageInfo2 = PIL.GetPageSetPageCtrlInfo("addb1000f18af156"); // trans_doc_totals
           // dsPageInfo2.Tables["Page"].Rows[0]["multiple_recs_page"] = true;
            DataSet dsPageInfo3 = PIL.GetPageSetPageCtrlInfo("629c0192a573ba67"); // tax_fields
            //dsPageInfo3.Tables["Page"].Rows[0]["multiple_recs_page"] = true;
            dsPageInfo.Tables["Page"].Merge(dsPageInfo1.Tables["Page"]);
            dsPageInfo.Tables["Page"].Merge(dsPageInfo2.Tables["Page"]);
            dsPageInfo.Tables["Page"].Merge(dsPageInfo3.Tables["Page"]);
            dsPageInfo.Tables["PageCtrl"].Merge(dsPageInfo1.Tables["PageCtrl"]);
            dsPageInfo.Tables["PageCtrl"].Merge(dsPageInfo2.Tables["PageCtrl"]);
            dsPageInfo.Tables["PageCtrl"].Merge(dsPageInfo3.Tables["PageCtrl"]);
            dsPageInfo.Tables["PageKeys"].Merge(dsPageInfo1.Tables["PageKeys"]);
            dsPageInfo.Tables["PageKeys"].Merge(dsPageInfo2.Tables["PageKeys"]);
            dsPageInfo.Tables["PageKeys"].Merge(dsPageInfo3.Tables["PageKeys"]);

            ScreenReader SR = new ScreenReader();
            SR.CreatePageDataTable(ds, dsePageInfo.Tables["Page"], dsePageInfo.Tables["PageCtrl"]);
            
        }
        catch(Exception ex)
        {
        }
        return ds;
    }

    private void FillInputData(DataSet dsePageInfo,DataSet dsInputInfo)
    {
        ScreenReader SR = new ScreenReader();
        try
        {
            if (dsePageInfo != null)
            {
                SR.CreatePageDataTable(dsInputInfo, dsePageInfo.Tables["Page"], dsePageInfo.Tables["PageCtrl"]);

            if(dsInputInfo.Tables["p_transaction_document"]!=null)
            {
                DataRow drr = dsInputInfo.Tables["p_transaction_document"].NewRow();
                drr["master_object"] = p_transaction_document_master_object.SelectedValue;
                drr["child_object"] = p_transaction_document_child_object.SelectedValue;
                drr["sort_by"] = p_transaction_document_sort_by.Text;
                drr["af_row_id"] = p_transaction_document_af_row_id.Value;
                drr["created_by"] = p_transaction_document_created_by.Value;
                drr["created_at"] = p_transaction_document_created_at.Value;
                drr["modified_at"] = DateTime.UtcNow;
                drr["modified_by"] = Session["user_afrowid"].ToString();
                drr["owner_id"] = p_transaction_document_owner_id.SelectedValue;
                if (Request.QueryString["PK"] == null || Request.QueryString["CPY"] != null)
                {
                    string af_row_id = SR.getHashKey();
                    drr["af_row_id"] = af_row_id;
                    p_transaction_document_af_row_id.Value = af_row_id;
                    drr["created_by"] = Session["user_afrowid"].ToString();
                    drr["created_at"] = DateTime.UtcNow;
                }
                dsInputInfo.Tables["p_transaction_document"].Rows.Add(drr);
            }
                if(dsInputInfo.Tables["p_trans_doc_totals"]!=null)
                {
                    DataRow drrtotals = dsInputInfo.Tables["p_trans_doc_totals"].NewRow();
                    drrtotals["trans_doc"] = p_transaction_document_af_row_id.Value;
                    drrtotals["page_id"] = p_transaction_document_child_object.SelectedValue;
                    drrtotals["attrib_id"] = DDChildFields.SelectedValue;
                    drrtotals["created_by"] = p_transaction_document_created_by.Value;
                    drrtotals["created_at"] = p_transaction_document_created_at.Value;
                    drrtotals["modified_by"] = Session["user_afrowid"].ToString();
                    drrtotals["owner_id"] = p_transaction_document_owner_id.SelectedValue;
                    drrtotals["modified_at"] = DateTime.UtcNow;
                    drrtotals["af_row_id"] = p_trans_doc_totals_af_row_id.Value;
                    if (Request.QueryString["PK"] == null || Request.QueryString["CPY"] != null)
                    {
                        drrtotals["af_row_id"] = SR.getHashKey();
                        drrtotals["created_by"] = Session["user_afrowid"].ToString();
                        drrtotals["created_at"] = DateTime.UtcNow;
                    }

                    dsInputInfo.Tables["p_trans_doc_totals"].Rows.Add(drrtotals);
                }
                if(dsInputInfo.Tables["p_trans_doc_grand_total"]!=null)
                {
                    DataRow drrgrandtotal = dsInputInfo.Tables["p_trans_doc_grand_total"].NewRow();
                    drrgrandtotal["trans_doc"] = p_transaction_document_af_row_id.Value;
                    drrgrandtotal["page_id"] = p_transaction_document_master_object.SelectedValue;
                    drrgrandtotal["grand_total_attrib_id"] = p_trans_doc_grand_total_grand_total_attrib_id.SelectedValue;
                    drrgrandtotal["associated_child_attrib_id"] = p_trans_doc_grand_total_associated_child_attrib_id.SelectedValue;
                    drrgrandtotal["seq"] = 1;
                    drrgrandtotal["active"] = p_trans_doc_grand_total_active.Checked;
                    drrgrandtotal["af_row_id"] = p_trans_doc_grand_total_af_row_id.Value;
                    drrgrandtotal["created_by"] = p_transaction_document_created_by.Value;
                    drrgrandtotal["created_at"] = p_transaction_document_created_at.Value;
                    drrgrandtotal["modified_by"] = Session["user_afrowid"].ToString();
                    drrgrandtotal["owner_id"] = p_transaction_document_owner_id.SelectedValue;
                    drrgrandtotal["modified_at"] = DateTime.UtcNow;
                    if (Request.QueryString["PK"] == null || Request.QueryString["CPY"] != null)
                    {
                        drrgrandtotal["af_row_id"] = SR.getHashKey();
                        drrgrandtotal["created_by"] = Session["user_afrowid"].ToString();
                        drrgrandtotal["created_at"] = DateTime.UtcNow;
                    }
                    dsInputInfo.Tables["p_trans_doc_grand_total"].Rows.Add(drrgrandtotal);

                }
                if (dsInputInfo.Tables["p_tax_fields"] != null)
                {
                    if(dd_tax_child_field1.SelectedIndex>0 || dd_tax_master_field1.SelectedIndex>0|| dd_tax_percent_field1.SelectedIndex>0|| dd_taxable_field1.SelectedIndex>0)
                    {
                        DataRow drrtaxfields = dsInputInfo.Tables["p_tax_fields"].NewRow();
                        drrtaxfields["master_object"] = p_transaction_document_af_row_id.Value;
                        drrtaxfields["percentage_field"] = dd_tax_percent_field1.SelectedValue;
                        drrtaxfields["master_field"] = dd_tax_master_field1.SelectedValue;
                        drrtaxfields["child_field"] = dd_tax_child_field1.SelectedValue;
                        drrtaxfields["taxable_field"] = dd_taxable_field1.SelectedValue;
                        drrtaxfields["master_page_id"] = p_transaction_document_master_object.SelectedValue;
                        drrtaxfields["child_page_id"] = p_transaction_document_child_object.SelectedValue;
                        drrtaxfields["seq"] = 1;
                        drrtaxfields["af_row_id"] = p_tax_fields_af_row_id_1.Value;
                        drrtaxfields["created_by"] = p_transaction_document_created_by.Value;
                        drrtaxfields["created_at"] = p_transaction_document_created_at.Value;
                        drrtaxfields["modified_by"] = Session["user_afrowid"].ToString();
                        drrtaxfields["owner_id"] = p_transaction_document_owner_id.SelectedValue;
                        drrtaxfields["modified_at"] = DateTime.UtcNow;
                        if (Request.QueryString["PK"] == null || Request.QueryString["CPY"] != null)
                        {
                            drrtaxfields["af_row_id"] = SR.getHashKey();
                            drrtaxfields["created_by"] = Session["user_afrowid"].ToString();
                            drrtaxfields["created_at"] = DateTime.UtcNow;
                        }
                        dsInputInfo.Tables["p_tax_fields"].Rows.Add(drrtaxfields);
                    }

                    if (dd_tax_child_field2.SelectedIndex > 0 || dd_tax_master_field2.SelectedIndex > 0 || dd_tax_percent_field2.SelectedIndex > 0 || dd_taxable_field2.SelectedIndex > 0)
                    {
                        DataRow drrtaxfields = dsInputInfo.Tables["p_tax_fields"].NewRow();
                        drrtaxfields["master_object"] = p_transaction_document_af_row_id.Value;
                        drrtaxfields["percentage_field"] = dd_tax_percent_field2.SelectedValue;
                        drrtaxfields["master_field"] = dd_tax_master_field2.SelectedValue;
                        drrtaxfields["child_field"] = dd_tax_child_field2.SelectedValue;
                        drrtaxfields["taxable_field"] = dd_taxable_field2.SelectedValue;
                        drrtaxfields["master_page_id"] = p_transaction_document_master_object.SelectedValue;
                        drrtaxfields["child_page_id"] = p_transaction_document_child_object.SelectedValue;
                        drrtaxfields["seq"] = 2;
                        drrtaxfields["af_row_id"] = p_tax_fields_af_row_id_2.Value;
                        drrtaxfields["created_by"] = p_transaction_document_created_by.Value;
                        drrtaxfields["created_at"] = p_transaction_document_created_at.Value;
                        drrtaxfields["modified_by"] = Session["user_afrowid"].ToString();
                        drrtaxfields["owner_id"] = p_transaction_document_owner_id.SelectedValue;
                        drrtaxfields["modified_at"] = DateTime.UtcNow;
                        if (Request.QueryString["PK"] == null || Request.QueryString["CPY"] != null)
                        {
                            drrtaxfields["af_row_id"] = SR.getHashKey();
                            drrtaxfields["created_by"] = Session["user_afrowid"].ToString();
                            drrtaxfields["created_at"] = DateTime.UtcNow;
                        }
                        dsInputInfo.Tables["p_tax_fields"].Rows.Add(drrtaxfields);
                    }


                }
                SetDefaultFields(ref dsInputInfo);
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : FillInputData :" + ex.Message);
        }
        finally
        {
        }
    }

    private void SetDefaultFields(ref DataSet dsInputInfo)
    {
        ScreenReader SR = new ScreenReader();
        try
        {
            if (dsInputInfo != null)
            if(dsInputInfo.Tables.Count>0)
                foreach (DataTable dt in dsInputInfo.Tables)
                {
                    if(dt!=null)
                        if(dt.Rows.Count>0)
                            if(dt.Columns.Contains("af_row_id"))
                                foreach (DataRow dr in dt.Rows)
                                {
                                    if (dr["af_row_id"].ToString().Trim() == "")
                                    {
                                        dr["af_row_id"] = SR.getHashKey();
                                        dr.AcceptChanges();
                                    }
                                }
                }

            
        }
        catch
        {
        }
    }

    private void ExecuteCommand()
    {
        HDDatabase HDD = new HDDatabase();
        try
        {
            if (Request.QueryString["PK"] != null)
            {
                string master_af_row_id = Request.QueryString["PK"].ToString();
                string sql = "delete from sy_trans_doc_totals where trans_doc = '" + master_af_row_id + "'";
                HDD.ExecuteCmdObj(new SqlCommand(sql));

                sql = "delete from sy_trans_doc_grand_total where trans_doc = '" + master_af_row_id + "'";
                HDD.ExecuteCmdObj(new SqlCommand(sql));

                sql = "delete from sy_transaction_document where af_row_id = '" + master_af_row_id + "'";
                HDD.ExecuteCmdObj(new SqlCommand(sql));

                sql = "delete from sy_tax_fields where master_object = '" + master_af_row_id + "'";
                HDD.ExecuteCmdObj(new SqlCommand(sql));
            }
        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("Error : ExecuteCommand :"+ ex.Message);
        }
        finally
        {
            HDD = null;
        }
    }
}
